package com.foreverwin.jzymes.production.cutter.service.impl;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.cutter.domain.ToolBreakLog;
import com.foreverwin.jzymes.production.cutter.enums.RecordStatusEnum;
import com.foreverwin.jzymes.production.cutter.mapper.ToolBreakAdiviceMapper;
import com.foreverwin.jzymes.production.cutter.service.IToolBreakAdiviceService;
import com.foreverwin.jzymes.production.cutter.service.IToolBreakLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IToolBreakAdiviceServiceImpl implements IToolBreakAdiviceService {

    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IResourceTypeAdviceService resourceTypeAdviceService;
    @Autowired
    IToolAdiviceService toolAdiviceService;
    @Autowired
    IFieldGroupAdviceService fieldGroupAdviceService;
    @Autowired
    ICustomerService customerService ;
    @Autowired
    IToolBreakLogService toolBreakLogService;
    @Autowired
    IItemGroupAdviceService iItemGroupAdviceService;
    @Autowired
    ToolBreakAdiviceMapper toolBreakAdiviceMapper;

    /**
     * 新增更换刀具信息
     * @param  toolBreakLog
     */
    @Override
    public void insertToolBreak( ToolBreakLog toolBreakLog ) {
        /**检查字段*/
        checkToolbreak( toolBreakLog );

        toolBreakLog.setCreatedDateTime( new Date());
        toolBreakLog.setStatus( RecordStatusEnum.Y.name() );

        toolBreakLogService.insertToolBreakLog( toolBreakLog );
    }

    /**
     * 更新刀具信息
     *
     * @param  toolBreakLog
     */
    @Override
    public void updateToolBreak( ToolBreakLog toolBreakLog ) {
        /**检查字段*/
        checkToolbreak( toolBreakLog );

       ToolBreakLog toolBreakLog1 = toolBreakLogService.selectToolBreakLogById( toolBreakLog.getHandle() );
       Assert.notNull( toolBreakLog1,I18nUtil.getI18nText("production.cutter.toolBreakLogNotExist") );

       toolBreakLog.setCreatedDateTime( toolBreakLog1.getCreatedDateTime() );
       toolBreakLog.setCreateUserBo( toolBreakLog1.getCreateUserBo() );

       toolBreakLogService.updateToolBreakLog( toolBreakLog );

    }

    /**
     *删除刀具信息
     * @param  list
     * */
    @Override
    public void deleteToolBreak( List< String >  list){

        /**list不能为空*/
        Assert.isTrue( list != null && list.size() > 0, I18nUtil.getI18nText( "production.cutter.toolBreakLogList" ) );

        for( int i = 0; i < list.size(); i++ ){
         ToolBreakLog toolBreakLog =  toolBreakLogService.selectToolBreakLogById( list.get(i) );

         toolBreakLog.setStatus( RecordStatusEnum.N.name() );
         toolBreakLogService.updateToolBreakLog( toolBreakLog );

        }

    }




    /**
     * 检索更换刀具信息
     * @param  jsonMap
     */
    @Override
    public List<ToolBreakLog>  selectToolBreak( Map<String, Object> jsonMap ) {
        String site = (String)jsonMap.get("site");
        String resource = (String)jsonMap.get("resrce");
        String machineType = (String)jsonMap.get("machineType");

        Date startTime = null;
        Date endTime = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ((String)jsonMap.get("startDate") != ""){
            try {
             startTime =  simpleDateFormat.parse( (String)jsonMap.get("startDate") );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ((String)jsonMap.get("endDate") != ""){
            try {
                endTime =  simpleDateFormat.parse( (String)jsonMap.get("endDate") );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<ToolBreakLog>  breakLogs = toolBreakAdiviceMapper.selectToolBreakByreport( site,machineType,resource,startTime,endTime );
        List<Map<String,Object>> rturList = new ArrayList<>();
        for ( ToolBreakLog breakLog : breakLogs ){
            String fieldLabel = toolBreakAdiviceMapper.selectfieldLabel( site , breakLog.getBreakType() );
            breakLog.setBreakType( fieldLabel );
        }

       /* for ( ToolBreakLog breakLog : breakLogs ){

            Map< String , Object > returnMap = new HashMap<>();
            returnMap.put("handle",breakLog.getHandle());
            returnMap.put("chgTime",breakLog.getChgTime());
            returnMap.put("machineType",breakLog.getMachineType());
            returnMap.put("shift",breakLog.getShift());
            returnMap.put("resrce",breakLog.getResrce());
            returnMap.put("cncType",breakLog.getCncType());
            returnMap.put("toolType",breakLog.getToolType());
            returnMap.put("toolLoc",breakLog.getToolLoc());
            returnMap.put("qty",breakLog.getQty());
            returnMap.put("maxUsage",breakLog.getMaxUsage());
            returnMap.put("actualUsage",breakLog.getActualUsage());
            returnMap.put("breakType",breakLog.getBreakType());
            returnMap.put("brand",breakLog.getBrand());
            returnMap.put("lotNo",breakLog.getLotNo());
            returnMap.put("technicion",breakLog.getTechnicion());
            returnMap.put("remark",breakLog.getRemark());
            returnMap.put("createdDateTime",breakLog.getCreatedDateTime());
            String fieldLabel = toolBreakAdiviceMapper.selectfieldLabel( site , breakLog.getBreakType() );
            returnMap.put("fieldLabel",fieldLabel );

            rturList.add( returnMap );
        }*/

       return  breakLogs ;
    }

    /**
     * 检验前台传入数据
     *
     * @param toolBreakLog
     * */
    public void  checkToolbreak(ToolBreakLog toolBreakLog){
        String site = toolBreakLog.getSite();
        String resrce = toolBreakLog.getResrce();
        String resourceType = toolBreakLog.getCncType();

        /**检查站点*/
        siteAdviceService.existSite( site );
        /**检查机种*/
        iItemGroupAdviceService.existItemGroup( site , toolBreakLog.getMachineType() );
        /**检查刀具类型*/
        toolAdiviceService.selectToolMap( toolBreakLog.getToolType() , site );
        /**班次不能为空*/
        Assert.notNull( toolBreakLog.getShift() , I18nUtil.getI18nText("production.labor.shift") );
        /**检查设备*/
        resourceAdviceService.existResource( site , resrce );
        /**检查夹位*/
        fieldGroupAdviceService.existFieldGroup( site , Constants.FIELD_GROUP_CncType , resourceType );
        /**检查夹位和设备关系*/
        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );

        Map<String,Object> rtnResourceMap = new HashMap<>();
        List<Map<String,String>> resourceList = resourceTypeAdviceService.findResourceList( site, resourceType );
        if( resourceList.contains( resourceMap ) ){
            rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
            rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
        }
        if( rtnResourceMap.isEmpty() ){
            throw BusinessException.build( "common.resourceNotMatchResourceType", new String[]{ resrce, resourceType } );
        }
        /**检查刀号*/
        fieldGroupAdviceService.existFieldGroup( site , Constants.FIELD_GROUP_ToolLoc , toolBreakLog.getToolLoc() );
        /**检查数量*/
        Assert.notNull(  toolBreakLog.getQty() , I18nUtil.getI18nText( "production.cutter.qty" ) );
        /**标准寿命*/
        Assert.notNull( toolBreakLog.getMaxUsage()  , I18nUtil.getI18nText( "production.cutter.actualUsage" ) );
        /**实际寿命*/
        Assert.notNull(  toolBreakLog.getActualUsage() , I18nUtil.getI18nText( "production.cutter.maxUsage" ) );
        /**换刀类型*/
        fieldGroupAdviceService.existFieldGroup( toolBreakLog.getSite() , Constants.FIELD_GROUP_BreakType, toolBreakLog.getBreakType() );
        /**生技员*/
       // customerService.existUser( toolBreakLog.getSite() , toolBreakLog.getTechnicion()  );
        Assert.hasText( toolBreakLog.getTechnicion() , I18nUtil.getI18nText("production.cutter.Technicion") );

    }
}
